package com.chalk.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.dao.VenueDao;
import com.chalk.exception.ColumnNotEmptyException;
import com.chalk.model.SportEvent;
import com.chalk.model.Venue;
import com.chalk.model.VenueAndSe;
import com.chalk.model.VenueComment;
import com.chalk.service.CollectionService;
import com.chalk.service.VenueAndSeService;
import com.chalk.service.VenueCommentService;
import com.chalk.service.VenueService;
import com.chalk.util.MapUtil;
import com.chalk.util.MpUtil;
import com.chalk.vo.VenueVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author glq
 * @since 2020-12-10
 */

@Service
public class VenueServiceImpl extends ServiceImpl<VenueDao, Venue> implements VenueService {

    @Autowired
    VenueAndSeService venueAndSeService;
    @Autowired
    CollectionService collectionService;
    @Autowired
    MpUtil<Venue> mpUtil;

    @Autowired
    VenueCommentService venueCommentService;

    @Override
    public Page<Venue> getVenueForPage(Map<String, Object> map) throws ColumnNotEmptyException {
        String venueName = (String) map.get("venueName");
        String venueSite = (String) map.get("venueSite");
        EntityWrapper<Venue> wrapper = mpUtil.getWrapper();
        wrapper.like(StringUtils.isNotBlank(venueName), "venue_name", venueName);
        wrapper.like(StringUtils.isNotBlank(venueSite), "venue_site", venueSite);
        Page<Venue> page = mpUtil.getPage(map);
        page.setDescs(mpUtil.getDesc("create_time"));
        return selectPage(page, wrapper);
    }


    @Override
    @Transactional
    public Boolean saveVenue(VenueVo venueVo) {
        Venue venue = new Venue();
        //将VenueVo对象拷贝到 venue对象上
        BeanUtils.copyProperties(venueVo, venue);
        venue.setCreateTime(LocalDateTime.now());
        Boolean f = insert(venue);
        if (f) {
            for (int i = 0; i < venue.getSeIds().size(); i++) {
                venueAndSeService.insert(new VenueAndSe(venue.getSeIds().get(i), venue.getVenueId()));
            }
        }
        return f;
    }

    @Override
    public Boolean updateVenue(VenueVo venueVo) {
        Venue venue = new Venue();
        BeanUtils.copyProperties(venueVo, venue);
        venue.setUpdateTime(LocalDateTime.now());
        Boolean f = updateById(venue);
        if (f) {
            venueAndSeService.deleteVenueAndSe(venue.getVenueId());
            venueAndSeService.batchSaveVenueAndSe(venueVo.getSeIds(), venue.getVenueId());
        }
        return f;
    }

    @Override
    @Transactional
    public Boolean deleteVenue(Integer[] venueIds) {
        Venue venue = new Venue();
        boolean b =false;
        for (int i = 0; i < venueIds.length; i++) {
            venue.setVenueId(venueIds[i]);
            venue.setIsDelete(1);
            b = updateById(venue);
            venueCommentService.deleteVenueCommentByVenueId(venueIds[i]);
        }
        return b;
    }

    @Override
    public Boolean setIsRecommend(Integer venueId) {
        Venue venue = new Venue();
        venue.setVenueId(venueId);
        venue.setIsRecommend(1);
        return updateById(venue);
    }
}
